/**
 * 
 * gislin
 * 2016年8月27日
 */
package com.szholly.pro.watergas_server.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 同比环比模型
 * @author gislin
 * 2016年8月27日
 */
public class Y2YM2MDateModel {
	
	private List<String> dateTextList;
	private Date startDate;
	private Date endDate;
	private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 构造函数
	 * @param endDate 结束时间
	 * @param dateLength 时间长度
	 * @param cycleTypeStr 周期类型 day,week,month,quarter,year
	 * @param isY2Y 是否是同比，同比为ture，环比为flase
	 * @throws ParseException
	 */
	public Y2YM2MDateModel(Date endDate,int dateLength,String cycleTypeStr,boolean isY2Y) throws ParseException{
		
		if(isY2Y){
			dateTextList = getY2YDateText(endDate, dateLength, cycleTypeStr);
		}
		else{
			dateTextList = getM2MDateText(endDate, dateLength, cycleTypeStr);
		}
		
		this.endDate = getEndDate(endDate, dateLength, cycleTypeStr);
		this.startDate = getStartDate(this.endDate, dateLength, cycleTypeStr,isY2Y);
		
		System.out.println("日期生成结果：\n"+"startDate:"+sf.format(this.startDate)+"\nendDate:"+sf.format(this.endDate));
		for(String dateText : dateTextList){
			System.out.println(dateText);
		}

	}
	
	public List<String> getDateTextList() {
		return dateTextList;
	}

	public void setDateTextList(List<String> dateTextList) {
		this.dateTextList = dateTextList;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * 计算开始时间
	 * gislin
	 * 2016年8月27日
	 * @param endDate
	 * @param dateLength
	 * @param cycleTypeStr
	 * @param isY2Y
	 * @return
	 */
	private Date getStartDate(Date endDate,int dateLength,String cycleTypeStr,boolean isY2Y){
		
		Calendar currentCal = Calendar.getInstance();
		currentCal.setTime(endDate);
		
		if(isY2Y){
			currentCal.add(Calendar.YEAR, -dateLength);
		}
		else{
			if("day".equals(cycleTypeStr)){
				currentCal.add(Calendar.DAY_OF_MONTH, -dateLength);
			}
			else if("week".equals(cycleTypeStr)){
				currentCal.add(Calendar.WEEK_OF_YEAR, -dateLength);
			}
			else if("month".equals(cycleTypeStr)){
				currentCal.add(Calendar.MONTH, -dateLength);
			}
			else if("quarter".equals(cycleTypeStr)){
				currentCal.add(Calendar.MONTH, -dateLength*3);
			}
			else if("year".equals(cycleTypeStr)){
				currentCal.add(Calendar.YEAR, -dateLength);
			}
		}
		return currentCal.getTime();
	}
	
	/**
	 * 计算结束时间
	 * gislin
	 * 2016年8月27日
	 * @param endDate
	 * @param dateLength
	 * @param cycleTypeStr
	 * @param isY2Y
	 * @return
	 */
	private Date getEndDate(Date endDate,int dateLength,String cycleTypeStr){
		
		Calendar currentCal = Calendar.getInstance();
		currentCal.setTime(endDate);
		int year = currentCal.get(Calendar.YEAR);
		int month = currentCal.get(Calendar.MONTH);
		int week = currentCal.get(Calendar.WEEK_OF_YEAR);
		
		if("week".equals(cycleTypeStr)){
			currentCal.set(Calendar.DAY_OF_WEEK,7);
			currentCal.set(Calendar.WEEK_OF_YEAR, week);
		}
		else if("month".equals(cycleTypeStr)){
			currentCal.set(Calendar.DAY_OF_MONTH, 1);
			currentCal.add(Calendar.MONTH,1);
			currentCal.add(Calendar.DAY_OF_MONTH,-1);
		}
		else if("quarter".equals(cycleTypeStr)){
			int newMonth = 0;
			if(month <= 3){
				newMonth = 2;
			}
			else if(month <= 6){
				newMonth = 5;
			}
			else if(month <= 9){
				newMonth = 8;
			}
			else if(month <= 12){
				newMonth = 11;
			}
			currentCal.set(Calendar.DAY_OF_MONTH, 1);
			currentCal.set(Calendar.MONTH, newMonth);
			currentCal.add(Calendar.MONTH,1);
			currentCal.add(Calendar.DAY_OF_MONTH,-1);
		}
		else if("year".equals(cycleTypeStr)){
			currentCal.set(year, 11, 31);
		}
		currentCal.set(Calendar.HOUR_OF_DAY,23);
		currentCal.set(Calendar.MINUTE,59);
		currentCal.set(Calendar.SECOND,59);
		return currentCal.getTime();
	}
	
	private List<String> getY2YDateText(Date endDate,int dateLength,String cycleTypeStr) throws ParseException{
		
		List<String> dateTexts = new ArrayList<>();
		List<String> resultDateTexts = new ArrayList<>();
		
		Calendar currentCal = Calendar.getInstance();
		currentCal.setTime(endDate);
		int year = currentCal.get(Calendar.YEAR);
		int month = currentCal.get(Calendar.MONTH);
		int day = currentCal.get(Calendar.DAY_OF_MONTH);
		int week = currentCal.get(Calendar.WEEK_OF_YEAR);
		int quarter = 0;
		
		if(month <= 3){
			quarter = 1;
		}
		else if(month <= 6){
			quarter = 2;
		}
		else if(month <= 9){
			quarter = 3;
		}
		else if(month <= 12){
			quarter = 4;
		}
		
		for(int i=0;i<dateLength;i++){
			
			String curMonth = "";
			if(month+1 < 10){
				curMonth = "0"+(month+1);
			}
			else{
				curMonth = month+1+"";
			}
			
			String curDay = "";
			if(day < 10){
				curDay = "0"+day;
			}
			else{
				curDay = day+"";
			}
			
			if("day".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + curMonth+"月" + curDay + "日");
			}
			else if("week".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + week+"周");
			}
			else if("month".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + curMonth+"月");
			}
			else if("quarter".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + quarter+"季");
			}
			else if("year".equals(cycleTypeStr)){
				dateTexts.add(year + "年");
			}
			year--;
		}
		
		for(int i=dateTexts.size()-1;i>=0;i--){
			resultDateTexts.add(dateTexts.get(i));
		}
		
		return resultDateTexts;
	}
	
	/**
	 * 获取环比时间点集
	 * gislin
	 * 2016年8月27日
	 * @param endDate
	 * @param dateLength
	 * @param cycleTypeStr day,week,month,quarter,year
	 * @return
	 * @throws ParseException
	 */
	private List<String> getM2MDateText(Date endDate,int dateLength,String cycleTypeStr) throws ParseException{
		
		List<String> dateTexts = new ArrayList<>();
		List<String> resultDateTexts = new ArrayList<>();
		
		Calendar currentCal = Calendar.getInstance();
		currentCal.setTime(endDate);
		
		//如果是季度的话，把月置为每个季度的第一个月
		if("quarter".equals(cycleTypeStr)){
			int month = currentCal.get(Calendar.MONTH);
			if(month <= 3){
				currentCal.set(Calendar.MONTH, 1);
			}
			else if(month <= 6){
				currentCal.set(Calendar.MONTH, 4);
			}
			else if(month <= 9){
				currentCal.set(Calendar.MONTH, 7);
			}
			else if(month <= 12){
				currentCal.set(Calendar.MONTH, 10);
			}
		}
		
		for(int i=0;i<dateLength;i++){
			
			int year = currentCal.get(Calendar.YEAR);
			int month = currentCal.get(Calendar.MONTH);
			int day = currentCal.get(Calendar.DAY_OF_MONTH);
			int week = currentCal.get(Calendar.WEEK_OF_YEAR);
			int quarter = getQuarter(month);
			
			String curMonth = "";
			if(month+1 < 10){
				curMonth = "0"+(month+1);
			}
			else{
				curMonth = month+1+"";
			}
			
			String curDay = "";
			if(day < 10){
				curDay = "0"+day;
			}
			else{
				curDay = day+"";
			}
			
			if("day".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + curMonth+"月" + curDay + "日");
				currentCal.add(Calendar.DAY_OF_MONTH, -1);
			}
			else if("week".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + week+"周");
				currentCal.add(Calendar.WEEK_OF_YEAR, -1);
			}
			else if("month".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + curMonth+"月");
				currentCal.add(Calendar.MONTH, -1);
			}
			else if("quarter".equals(cycleTypeStr)){
				dateTexts.add(year + "年" + quarter+"季");
				currentCal.add(Calendar.MONTH, -3);
			}
			else if("year".equals(cycleTypeStr)){
				dateTexts.add(year + "年");
				currentCal.add(Calendar.YEAR, -1);
			}
		}
		for(int i=dateTexts.size()-1;i>=0;i--){
			resultDateTexts.add(dateTexts.get(i));
		}
		
		return resultDateTexts;
	}
	
	/**
	 * 根据月份获取季度
	 * gislin
	 * 2016年8月27日
	 * @param month
	 * @return
	 */
	private int getQuarter(int month){
		int quarter = 0;
		if(month <= 3){
			quarter = 1;
		}
		else if(month <= 6){
			quarter = 2;
		}
		else if(month <= 9){
			quarter = 3;
		}
		else if(month <= 12){
			quarter = 4;
		}
		return quarter;
	}
}
